#!/bin/bash

# Copyright (c) Los Alamos National Security, LLC, and others.

# This script downloads all the components necessary to run manual-install.

# WARNING: googlecode.com does not work with wget -N for some reason,
# returning 404. That's why some URLs below avoid wget_. It's an old bug that
# Google is ignoring: <http://code.google.com/p/support/issues/detail?id=660>.
# The workaround is to use --continue, which is considerably more error prone.

set -e
set -x

# directories
cd $(dirname $0)/..
mkdir -p deps/src
mkdir -p deps/packages

# commands
WGET='wget -N'
WGET_S3='wget --continue'  # URLs redirecting to S3 fail 403 if the file exists
PIP2PI="pip2pi -nz deps/packages"

# non-pip source files
pushd deps/src
$WGET http://www.python.org/ftp/python/3.4.3/Python-3.4.3.tgz
$WGET_S3 http://github.com/rogerbinns/apsw/releases/download/3.8.10.1-r1/apsw-3.8.10.1-r1.zip
$WGET http://download.osgeo.org/gdal/1.11.2/gdal-1.11.2.tar.gz
$WGET http://download.osgeo.org/geos/geos-3.4.2.tar.bz2
$WGET http://download.osgeo.org/proj/proj-4.9.1.tar.gz
$WGET https://www.sqlite.org/2015/sqlite-autoconf-3081002.tar.gz
$WGET http://prdownloads.sourceforge.net/swig/swig-3.0.6.tar.gz
# cmdtest and friends aren't on PyPI. Versions to match Ubuntu Vivid.
$WGET http://code.liw.fi/debian/pool/main/c/cmdtest/cmdtest_0.12.orig.tar.gz
$WGET http://code.liw.fi/debian/pool/main/p/python-cliapp/python-cliapp_1.20140719.orig.tar.gz
$WGET http://code.liw.fi/debian/pool/main/p/python-ttystatus/python-ttystatus_0.23.orig.tar.gz
popd

# pips

# Some libraries try to build (or something) during egg_info, blarrrgh.
# Therefore, we download those manually. If this wasn't a problem, we could
# just use pip2pi instead of pip2tgz and then dir2pi.

egrep -v '^(matplotlib|pandas|scipy)' requirements.txt > deps/requirements.txt
# Unclear why we need pbr (it doesn't get installed with the normal method),
# but here it is.
echo 'pbr' >> deps/requirements.txt
pip2tgz deps/packages --no-use-wheel -r deps/requirements.txt GDAL==1.11.2
pushd deps/packages
$WGET https://pypi.python.org/packages/source/p/pandas/pandas-0.16.2.tar.gz
$WGET https://downloads.sourceforge.net/project/matplotlib/matplotlib/matplotlib-1.4.3/matplotlib-1.4.3.tar.gz
$WGET http://sourceforge.net/projects/scipy/files/scipy/0.15.1/scipy-0.15.1.tar.gz
# Some installations of Python can't deal with bz2 archives, so make sure we
# have the .tar.gz too.
$WGET https://pypi.python.org/packages/source/p/pytz/pytz-2015.4.tar.gz
popd
dir2pi -n deps/packages
# work around https://github.com/wolever/pip2pi/issues/45
mv deps/packages/simple/python-daemon deps/packages/simple/python-daemon-3k

# create a tarball
tar czf deps.tar.gz deps/src deps/packages
